﻿using IconYuan.Model.DTO.Products;
using Core.Configuration.Api;
using Microsoft.AspNetCore.Mvc;
using IconYuan.Manager.Products;
using SqlSugar;

namespace IconYuan.Api.Controllers.Products
{
    /// <summary>
    /// 商品分类操作
    /// </summary>
    public class CategoryController : ApiBase
    {
       readonly CateManager cateSvc;
        public CategoryController()
        {
            cateSvc = new CateManager();

        }

        /// <summary>
        /// 分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<CateListDto> List()
        {
            var list = await cateSvc.GetListAsync(x => x.status == 1);
            var result = new CateListDto();
            result.list = list.Where(x => x.parent_id == 0).Select(x => new CateDto()
            {
                category_id = x.category_id,
                create_time = x.create_time,
                update_time = x.update_time,
                image_id = x.image_id,
                parent_id = x.parent_id,
                store_id = x.store_id,
                name = x.name,
                sort = x.sort,
                status = x.status,
                children = list.Where(y => y.parent_id == x.category_id).ToList()
            }).OrderBy(x => x.sort).ToList();
            return result;
        }
    }
}
